package cn.wolfcode.shop.seckill.web.frontend;

import cn.wolfcode.cloud.redis.RedisService;
import cn.wolfcode.shop.member.domain.User;
import cn.wolfcode.shop.seckill.redis.SeckillKeyPrefix;
import cn.wolfcode.shop.seckill.util.VerifyCodeImgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.OutputStream;

@Controller
public class VerifyCodeController {
    @Autowired
    private RedisService redisService;

    @RequestMapping("/createVeryCode")
    public void createVeryCode(String uuid, HttpServletResponse response) throws Exception {
        //生成验证码  8-5
        String code = VerifyCodeImgUtil.generateVerifyCode();
        //计算的验证码结果  3
        Integer result = VerifyCodeImgUtil.calc(code);
        //放到缓存中
        redisService.set(SeckillKeyPrefix.SECKILLVERIFYCODERESULT,uuid,result);
        //响应给页面
        BufferedImage verifyCodeImg = VerifyCodeImgUtil.createVerifyCodeImg(code);
        //写出到页面的图片
        ImageIO.write(verifyCodeImg,"JPEG",response.getOutputStream());
    }
}